# RAG智能问答系统 - Makefile
# 提供常用命令的快捷方式

.PHONY: help install run ingest clean docker-build docker-run docker-stop test

# 默认目标：显示帮助信息
help:
	@echo "=================================="
	@echo "RAG智能问答系统 - 可用命令"
	@echo "=================================="
	@echo ""
	@echo "基础命令："
	@echo "  make install       - 安装所有依赖"
	@echo "  make run          - 启动Web服务"
	@echo "  make ingest       - 运行文档摄取"
	@echo "  make clean        - 清理缓存文件"
	@echo ""
	@echo "Docker命令："
	@echo "  make docker-build - 构建Docker镜像"
	@echo "  make docker-run   - 启动Docker容器"
	@echo "  make docker-stop  - 停止Docker容器"
	@echo ""
	@echo "其他命令："
	@echo "  make test         - 运行测试"
	@echo "  make check-gpu    - 检查GPU配置"
	@echo ""

# 安装依赖
install:
	@echo "📦 安装Python依赖..."
	pip3 install -r requirements.txt
	@echo "✅ 依赖安装完成"

# 启动Web服务
run:
	@echo "🚀 启动RAG问答系统Web服务..."
	python3 app.py

# 运行文档摄取
ingest:
	@echo "📂 运行文档摄取流程..."
	python3 rag_document_ingestion.py

# 清理缓存文件
clean:
	@echo "🧹 清理缓存文件..."
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete
	find . -type f -name "*.pyo" -delete
	find . -type f -name "*.log" -delete
	@echo "✅ 清理完成"

# 构建Docker镜像
docker-build:
	@echo "🐳 构建Docker镜像..."
	docker build -t rag-qa-system:latest .
	@echo "✅ 镜像构建完成"

# 使用Docker Compose启动
docker-run:
	@echo "🐳 启动Docker容器..."
	docker-compose up -d
	@echo "✅ 容器已启动，访问 http://localhost:5000"

# 停止Docker容器
docker-stop:
	@echo "🛑 停止Docker容器..."
	docker-compose down
	@echo "✅ 容器已停止"

# 运行测试
test:
	@echo "🧪 运行测试..."
	python3 -m pytest tests/ -v 2>/dev/null || echo "⚠️  未找到测试文件"

# 检查GPU配置
check-gpu:
	@echo "🔍 检查GPU配置..."
	python3 config_gpu.py

# 设置脚本权限
permissions:
	@echo "🔧 设置脚本执行权限..."
	chmod +x 启动前端服务.sh
	chmod +x *.py
	@echo "✅ 权限设置完成"

# 查看系统状态
status:
	@echo "📊 系统状态检查..."
	@echo ""
	@echo "Python版本："
	@python3 --version
	@echo ""
	@echo "pip版本："
	@pip3 --version
	@echo ""
	@echo "向量数据库："
	@if [ -d "chroma_db" ]; then \
		echo "✅ 已创建"; \
	else \
		echo "❌ 未创建，请运行 make ingest"; \
	fi
	@echo ""
	@echo "BGE-M3模型："
	@if [ -d "bge-m3" ]; then \
		echo "✅ 已存在"; \
	else \
		echo "❌ 未找到"; \
	fi

# 创建虚拟环境
venv:
	@echo "🔧 创建Python虚拟环境..."
	python3 -m venv venv
	@echo "✅ 虚拟环境创建完成"
	@echo ""
	@echo "激活虚拟环境："
	@echo "  source venv/bin/activate"

# 查看日志
logs:
	@echo "📋 查看最近的日志..."
	@tail -n 50 *.log 2>/dev/null || echo "暂无日志文件"

# 快速启动（安装依赖并运行）
quickstart: install permissions ingest run

# 完整清理（包括数据库）
clean-all: clean
	@echo "⚠️  警告：即将删除向量数据库"
	@read -p "确认删除？(yes/no): " confirm; \
	if [ "$$confirm" = "yes" ]; then \
		rm -rf chroma_db; \
		echo "✅ 向量数据库已删除"; \
	else \
		echo "❌ 已取消"; \
	fi


